<template>
  <view class="pusher">
    <nav-back title="推送设置"></nav-back>
    <form v-if="pusher">
      <view class="cu-form-group">
        <view class="title">关注赛事提醒</view>
        <switch color="#fb7299" @change="switchChangeSet" data-name="matchAlert" :class="pusher.matchAlert ? 'checked' : ''" :checked="pusher.matchAlert ? true : false"></switch>
      </view>
      <view class="cu-form-group">
        <view class="title">竞猜赛事比分推送</view>
        <switch
          color="#fb7299"
          @change="switchChangeSet"
          data-name="matchScoreAlert"
          :class="pusher.matchScoreAlert ? 'checked' : ''"
          :checked="pusher.matchScoreAlert ? true : false"
        ></switch>
      </view>
      <view class="cu-form-group">
        <view class="title">商城物流推送</view>
        <switch
          color="#fb7299"
          @change="switchChangeSet"
          data-name="goodDeliverAlert"
          :class="pusher.goodDeliverAlert ? 'checked' : ''"
          :checked="pusher.goodDeliverAlert ? true : false"
        ></switch>
      </view>
      <view class="cu-form-group">
        <view class="title">阳光值满提醒</view>
        <switch
          color="#fb7299"
          @change="switchChangeSet"
          data-name="sunShineFullAlert"
          :class="pusher.sunShineFullAlert ? 'checked' : ''"
          :checked="pusher.sunShineFullAlert ? true : false"
        ></switch>
      </view>
      <view class="cu-form-group">
        <view class="title">会员续费提醒</view>
        <switch
          color="#fb7299"
          @change="switchChangeSet"
          data-name="vipEndAlert"
          :class="pusher.vipEndAlert ? 'checked' : ''"
          :checked="pusher.vipEndAlert ? true : false"
        ></switch>
      </view>
    </form>
  </view>
</template>

<script>
import user from "@/api/modules/user";
import navBack from "@/components/navBack";
import uniApp from "@/utils/uni-app";
export default {
  components: {
    navBack
  },
  data() {
    return {
      pusher: null
    };
  },
  onLoad() {
    user.getConfig().then(_ => {
      this.pusher = _.data.data;
      this.$forceUpdate();
    });
  },
  methods: {
    switchChangeSet({ currentTarget, detail }) {
      let name = currentTarget.dataset.name;
      user
        .setConfig(name, detail.value ? 1 : 0)
        .then(_ => {
          uniApp.toast(_.data.msg || "设置成功");
        })
        .catch(e => {
          uniApp.alert(e.message || e.data.msg || "设置失败");
        });
    }
  }
};
</script>
<style></style>
